package com.zlzlib.libui.weight.edittext;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatAutoCompleteTextView;
import androidx.core.content.res.ResourcesCompat;

import com.zlzlib.libui.R;
import com.zlzlib.libui.util.PxUtil;

/**
 * @Desc: 右边带清空按钮的编辑框
 * @Copyright:
 * @DateTime: 2020/3/23 16:20
 * @Author zlz
 * @Version 1.0
 */
public class EditAutoClearText extends AppCompatAutoCompleteTextView implements TextWatcher, View.OnFocusChangeListener {

    /**
     * 右边的图片
     */
    private Drawable clearDrawable;
    /**
     * 是否获取焦点
     */
    private boolean hasFocus = false;

    public EditAutoClearText(Context context) {
        super(context);
        initView();
    }

    public EditAutoClearText(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        clearDrawable = ResourcesCompat.getDrawable(getResources(), R.mipmap.close, null);
        if (clearDrawable != null) {
            clearDrawable.setBounds(0, 0, PxUtil.dip2px(getContext(), 15), PxUtil.dip2px(getContext(), 15));
        }
        setClearDrawableVisible(false);
        // 设置焦点变化监听
        setOnFocusChangeListener(this);
        // 设置文字输入变化监听
        addTextChangedListener(this);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            int handUpCoordinateX = (int) event.getX();
            if ((getWidth() - handUpCoordinateX) <= getCompoundPaddingRight()) {
                if (!TextUtils.isEmpty(getText())) {
                    setText("");
                    setClearDrawableVisible(false);
                }
            }
        }
        return super.onTouchEvent(event);
    }

    /**
     * 设置清除按钮是否可见
     *
     * @param visible true:可见,false:不可见
     */
    public void setClearDrawableVisible(boolean visible) {
        Drawable clear = visible ? clearDrawable : null;
        setCompoundDrawables(getCompoundDrawables()[0],
                getCompoundDrawables()[1], clear, getCompoundDrawables()[3]);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        setClearDrawableVisible(hasFocus && !TextUtils.isEmpty(s));
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        this.hasFocus = hasFocus;
        setClearDrawableVisible(hasFocus && !TextUtils.isEmpty(getText()));
    }
}
